package day6.HomeWork;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

//b.通过Map集合实现 打印购物小票
//   输入购买的商品名称 再输入购买商品的花费的金额
//   最后统计结果 如下:
//   商品名称   个数     总价
//     烤鸭      2      100
//     鞋子      2      500
//     ....
//   总价: 600
public class B {
    public static void main(String[] args) {
        Map<String, Product> map = new HashMap<>();
        Scanner sc = new Scanner(System.in);
        while (true) {
            System.out.println("请输入购买的商品名称,esc退出:");
            String name = sc.next();
            if ("esc".equals(name)) break;
            System.out.println("请输入购买的商品数量:");
            int num = sc.nextInt();
            double price = 0;
            if (map.containsKey(name)) {    //买过了:单价就用原来的  购买的数量要累加
                price = map.get(name).price;
                num = map.get(name).num + num;
            } else {    //没买过:单价手动输入  购买数量就是输入的数量
                System.out.println("请输入购买的商品价格:");
                price = sc.nextDouble();
            }
            map.put(name, new Product(name, num, price));
        }
        System.out.println("购买结束,商品清单如下:");
        System.out.println("商品名称\t个数\t总价");
        double sum = 0;
        for (String key : map.keySet()) {
            Product value = map.get(key);
            sum += value.num * value.price;
            System.out.println(key + "\t" + value.num + "\t" + (value.num * value.price));
        }
        System.out.println("总价:" + sum);
    }
}

class Product {
    String name;
    Integer num;
    double price;

    public Product(String name, Integer num, double price) {
        this.name = name;
        this.num = num;
        this.price = price;
    }

    @Override
    public String toString() {
        return "Product{" +
                "name='" + name + '\'' +
                ", num=" + num +
                ", price=" + price +
                '}';
    }
}